<?php
/**
 * This is the model class for table "user".
 *
 * The followings are the available columns in table 'user':
 * @property integer $id
 * @property string $email
 * @property string $password
 * @property string $name
 * @property string $address
 * @property string $phone
 */
class User extends CActiveRecord {

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return User the static model class
     */
    public $repass;
    public $rememberMe;
    public $verifyCode;

    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'user';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('email, password, name, address, phone', 'required'),
            array('email', 'email'),
            array('email', 'unique','message'=>'Email đã tồn tại trong hệ thống!'),
            array('password', 'length', 'min' => 6),
            array('phone', 'length', 'min' => 9),
            array('email, password, name, address, phone', 'length', 'max' => 255),
            array('role', 'length', 'max' => 5),
            array('repass', 'compare', 'compareAttribute' => 'repass'),
            array('rememberMe', 'boolean'),
            // verifyCode needs to be entered correctly
            array('verifyCode', 'captcha', 'allowEmpty' => !CCaptcha::checkRequirements()),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, email, password, name, address, phone, role', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'carts' => array(self::HAS_MANY, 'Cart', 'user_id'),
        );
    }

    protected function beforeSave() {
        if ($this->isNewRecord) {
            // $this->password = md5($this->password);
        }
        return true;
    }

    public function login() {
        $duration = $this->rememberMe ? 3600 * 24 * 30 : 0; // 30 days
        $identity = new UserIdentity($this->email, $this->password);
        $result = $identity->authenticate();
        if ($result) {
            Yii::app()->user->login($identity, $duration);
            return $result;
        } else {
            return false;
        }
    }

    public function getAdmin() {
        $criteria = new CDbCriteria;
        $criteria->alias = 'u';
        $criteria->distinct = true;
        $criteria->condition = "u.role =:role";
        $criteria->params = array(':role' => 'admin');
        $user = User::model()->find($criteria);
        return $user;
    }

    public function getGroupEmail() {
        $criteria = new CDbCriteria;
        $criteria->alias = 'u';
        $criteria->distinct = true;
        $criteria->condition = "u.role =:role";
        $criteria->params = array(':role' => 'groupemail');
        $user = User::model()->find($criteria);
        return $user;
    }

    public function getUserInformation($userName) {
        $criteria = new CDbCriteria;
        $criteria->alias = 'u';
        $criteria->distinct = true;
        $criteria->condition = "u.email =:email";
        $criteria->params = array(':email' => $userName);
        $user = User::model()->find($criteria);
        return $user;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'email' => Yii::t('default', 'Email'),
            'password' => Yii::t('default', 'Password'),
            'name' => Yii::t('default', 'Name'),
            'address' => Yii::t('default', 'Address'),
            'phone' => Yii::t('default', 'Phone'),
            'role' => Yii::t('default', 'Role'),
            'repass' => Yii::t('default', 'Repass'),
            'rememberMe' => Yii::t('default', 'Remember me'),
            'verifyCode' => Yii::t('default', 'Verify code'),
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('email', $this->email, true);
        $criteria->compare('password', $this->password, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('address', $this->address, true);
        $criteria->compare('phone', $this->phone, true);
        $criteria->compare('role', $this->role, true);

        return new CActiveDataProvider($this, array(
                    'criteria' => $criteria,
                ));
    }
}